<?php
if(defined("_PARA_FORUM"))
    return;
define("_PARA_FORUM", 1);

require_once "Thread.php";
require_once "HTMLable.php";

class Forum extends HTMLable {

    public $threads   = null;

    function Forum( $threads, $messages ) {
        if($threads != null) {
            $this->thread_them( $threads, $messages );
        }
        else {
            $this->threads = null;
        }
    }


    function get_threads() {
        return $this->threads;
    }


    private function thread_them( $threads, $messages ) {
        
        foreach( $threads as $thread ) {
            foreach ( $messages as $message ) {
                $thread->add_message( $message );                
            }
        }

        $this->threads = $threads;
    }


    private function add_thread_from_msg( $message ) {
        $thread = new Thread( $message );
        array_unshift( $this->threads, $thread );
    }


    public function find_thread( $id ) {
        foreach ($this->threads as $thread) {
            if($thread->id == $id)
                return $thread;
        }
        return null;
    }


    public function add_bodies_to_thread( $t_id, $bodies ) {
        $thread = $this->find_thread( $t_id );

        if(!$thread)
            return null;
        
        return $thread->add_bodies( $bodies );
    }


    public function htmlize( $options ) {
        echo "<div class=\"para_forum_forum\">\n";
        $opt2 = array();

        if( !$options['tview'] ) {
            $oc = "onclick=\"para_forum_uev_create_thread()\"";
            echo "<div id=\"para_forum_ctrls\">",
                "<span id=\"para_forum_ctrls_new_thread\" $oc>Ny tråd</span>",
                "</div>";
        }

        if( !$this->threads ) {
            echo "<div>This forum is empty</div>\n";            
        }
        else {
            foreach( $this->threads as $thread ) {                
                if( $options['tview'] == $thread->id )
                    $opt2['expanded'] = true;
                else
                    $opt2['expanded'] = false;

                $thread->htmlize( $opt2 );
            }
        }
        echo "</div>\n";
    }

    
    public function adm_htmlize( $options ) {
        echo "<div class=\"padm_forum\">";

        if( !$this->threads ) {
            echo "<div class=\"padm_info\">This forum is empty</div>\n";            
        }
        else {
            echo '<form method="post" action="?sec=forum&ssec=tview&do=form_action">', "\n";
            
            $this->adm_buttonrow();
            
            foreach( $this->threads as $thread ) {                
                $opt2 = array();
                $thread->adm_htmlize( $opt2 );
            }

            $this->adm_buttonrow();
            
            echo "</form>";
        }
        echo "<div>\n";
    }


    private function adm_buttonrow() {
        echo '<div class="padm_forum_buttons">', "\n";
        echo '<input class="button" type="reset"  name="reset" value="Reset"/>', "\n";
        echo '<input class="button" type="submit" name="delete" value="Delete"/>', "\n";
        echo "</div>\n";
    }

}

?>